Page({
  data: {
    current_page:1,
    list:[],
    flag:0
  },

  onLoad(opts) {
    let that = this
    console.log(opts)
    that.setData({
      c_name:opts.c_name,
      g_name:opts.g_name
    })
    wx.showLoading({
      title: '加载中...',
    })
  },
  
  onShow() {
    let that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页
    console.log(currPage.data)
    if(currPage.data.status == 1){
      that.setData({
        current_page:1
      })
    }
    console.log(that.data.current_page)
    that.loadlist(that.data.current_page)
  },

  loadlist(current_page){
    let that = this
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/personlist',
      data: {
        access_token: access_token,
        current_page:current_page,
        page_size:15
      },
      success: (res) => {
        console.log(res)
        console.log(that.data.current_page)
        if (res.data.code == 1) {
          if(that.data.current_page > 1){
            // 已经渲染到页面上的list
            let list = that.data.list
            that.setData({
              list:list.concat(res.data.list),
              current_page:current_page + 1
            })
          } else {
            that.setData({
              list:res.data.list,
              current_page:current_page + 1
            })
          }
          wx.hideLoading()
        }
      },
    })
  },

  delete(e) {
    let that = this
    that.setData({
      current_page:1
    })
    let id = e.currentTarget.dataset.id
    let access_token = wx.getStorageSync('access_token')
    wx.showModal({
      content: '确定要删除该信息吗？',
      success: (res) => {
        if (res.confirm) {
          console.log('点击删除')
          wx.request({
            url: 'https://baokang.wshoto.com/wx/basicdel',
            data: {
              access_token: access_token,
              type: 2,
              id: id
            },
            success: (res) => {
              console.log(res)
              if (res.data.code == 1) {
                wx.showToast({
                  title: '删除成功',
                })
                that.loadlist(that.data.current_page)
              }
            }
          })
        }
      }
    })
  },

  add(){
    wx.navigateTo({
      url: '/pages/population-add/population-add',
    })
  },

  edit(e){
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/population/population?id=' + id,
    })
  },

  query(e) {
    console.log(e)
    this.setData({
      list:[],
      current_page:1,
      name: e.detail.value,
      flag:1
    })
    let that = this
    that.queryList(that.data.current_page)
    if(e.detail.value == ""){
      that.loadlist(that.data.current_page)
    }
    // let name = that.data.name;
    // let access_token = wx.getStorageSync('access_token')
    // wx.request({
    //   url: 'https://baokang.wshoto.com/wx/personlist',
    //   data: {
    //     access_token: access_token,
    //     current_page:current_page,
    //     page_size:15,
    //     name: name
    //   },
    //   success: (res) => {
    //     console.log(res)
    //     console.log(that.data.list)
    //     console.log(that.data.current_page)
    //     if (res.data.code == 1) {
    //       that.setData({
    //         list:res.data.list
    //       })
    //       return
    //       if(that.data.current_page > 1){
    //         // 已经渲染到页面上的list
    //         let list = that.data.list
    //         that.setData({
    //           list:list.concat(res.data.list),
    //           current_page:current_page + 1
    //         })
    //       } else {
    //         that.setData({
    //           list:res.data.list,
    //           current_page:current_page + 1
    //         })
    //       }
    //     }
    //   }
    // })
  },

  queryList(current_page){
    let that = this
    let name = that.data.name;
    let access_token = wx.getStorageSync('access_token')
    wx.request({
      url: 'https://baokang.wshoto.com/wx/personlist',
      data: {
        access_token: access_token,
        current_page:current_page,
        page_size:15,
        name: name
      },
      success: (res) => {
        console.log(res)
        console.log(that.data.list)
        console.log(that.data.current_page)
        if (res.data.code == 1) {
          if(that.data.current_page > 1){
            // 已经渲染到页面上的list
            let list = that.data.list
            that.setData({
              list:list.concat(res.data.list),
              current_page:current_page + 1
            })
          } else {
            that.setData({
              list:res.data.list,
              current_page:current_page + 1
            })
          }
        }
      }
    })
  },

  onReachBottom(){
    let that = this
    console.log('上拉加载更多')
    if(that.data.flag == 0){
      that.loadlist(that.data.current_page)
    } else {
      console.log(that.data.current_page)
      that.queryList(that.data.current_page)
    }
  },

})
